home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / SAMLIB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-01  |  4.4 KB  |  167 lines

  1. #include "config.h"
  2. #ifdef SAMCALLB
  3. /*
  4.  * samlib.c
  5.  *
  6.  * This contains functions to init and access the API contained
  7.  * in resident program SAMAPI.EXE.
  8.  *
  9.  * SAMAPI.EXE is a resident program that provides an application program
  10.  * interface to the SAM Amateur Radio Callsign Database.
  11.  *
  12.  * Three functions in here:
  13.  *  LocateSam     - determines SAMAPI is loaded
  14.  *  CallSam       - call the API, passing a command parameter block and
  15.  *                  a buffer for result/response.
  16.  *  SetCountrySam - set the country for search, 0 = USA, 1 = Canada
  17.  *                  Note that normally automatic for simple call lookup
  18.  *
  19.  * The API is accessed via int 2fh, ah = dynamic muxid (default is 0x99).
  20.  * This is commonly called the "multiplex interrupt" interface.
  21.  * Per convention, int 2fh, ah = muxid, al = 0 is an install check, and
  22.  * al comes back non-zero if the API is installed.  For other API
  23.  * functions:
  24.  *
  25.  *      ah = muxid
  26.  *      al = 1
  27.  *   ds:si = pointer to command block
  28.  *   es:di = pointer to result block
  29.  *
  30.  * No registers are changed by SAMAPI (except al when input al == 0)
  31.  *
  32.  * See samapi.h for details on the command/response data structures,
  33.  * command codes, error codes, and so on.
  34.  *
  35.  * LocateSam looks for an environment string: SAMAPI=number.
  36.  * If found, "number" becomes the muxid.  SAMAPI.EXE does the same thing
  37.  * when it loads so muxid can be changed when conflicts arise.
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <dos.h>
  44.  
  45. #include "samapi.h"
  46.  
  47.  
  48. #if !defined(_lint)
  49. static char rcsid[] OPTIONAL = "$Id: samlib.c,v 1.7 1997/06/01 22:10:46 root Exp root $";
  50. #endif
  51.  
  52. int SamMuxid = DEFAULT_SAMMUX;
  53. int SamCountry = 0;                /* 0 == USA, 1 == Canada */
  54.  
  55. /**************************
  56.  * LocateSam
  57.  **************************
  58.  *
  59.  * Finds the resident SAMAPI code.  This MUST be called before
  60.  * other functions are used!.  It checks for the environment
  61.  * string SAMAPI=number to see if there is an override for the
  62.  * muxid (SAMAPI.EXE does the same thing).
  63.  *
  64.  * returns: 0 if resident code found and can be used, -1 if not
  65.  */
  66.  
  67. int LocateSam(void)
  68. {
  69.     int v;
  70.     char *p;
  71.     static union REGS r;
  72.  
  73.     p = getenv("SAMAPI");
  74.     if (p != NULL)
  75.     {
  76.         v = atoi(p);
  77.         if (v > 0 && v < 255)
  78.             SamMuxid = v;
  79.     }
  80.     r.h.al = 0;
  81.     r.h.ah = SamMuxid;
  82.     int86(0x2f, &r, &r);
  83.     return (r.h.al == 1) ? 0 : -1;
  84. }
  85.  
  86. /**************************
  87.  * CallSam
  88.  **************************
  89.  *
  90.  * Call the resident SAMAPI code.
  91.  *
  92.  * This expects a filled-in (all but header) command buffer
  93.  * and a to-be-filled-in response buffer.  cmd is stuffed into
  94.  * cmdbuf and the resident code is called (via int 0x2f).
  95.  *
  96.  * returns: error code from response buffer header (0 usually
  97.  *          means no error)
  98.  */
  99.  
  100. int CallSam(int cmd, void *cmdbuf, void *rspbuf)
  101. {
  102.     static union REGS r;
  103.     static struct SREGS sr;
  104.     int i;
  105.  
  106.     ((chdr_t *) cmdbuf)->cmd = cmd;
  107.  
  108.     /* make reserved fields 0 for future compatability. */
  109.     for (i = 0; i < 2; i++)
  110.         ((chdr_t *) cmdbuf)->fill[i] = 0;
  111.  
  112.     /* select which database */
  113.     ((chdr_t *) cmdbuf)->country = SamCountry;
  114.  
  115.     r.x.si = FP_OFF(cmdbuf);        /* ds:si is ptr to command */
  116.     sr.ds = cmdbuf;
  117.     r.x.di = FP_OFF(rspbuf);        /* es:di is ptr to result buf */
  118.     sr.es = rspbuf;
  119.     r.h.al = 1;
  120.     r.h.ah = SamMuxid;                /* ax = (muxid << 8) + 1 */
  121.     int86x(0x2f, &r, &r, &sr);        /* do the int 0x2f */
  122.     return ((rhdr_t *) rspbuf)->err;    /* return error code in rsp buf */
  123. }
  124.  
  125. /**************************
  126.  * SetCountrySam
  127.  **************************
  128.  *
  129.  * Selects the country for subsequent operations.  i.e., selects database
  130.  *
  131.  *  0 = USA
  132.  *  1 = Canada
  133.  *
  134.  * returns 0 if success, else !0 if error.  Possible errors are
  135.  * illegal country code or Canada database not installed.
  136.  *
  137.  * Note that for lookup by call, country selection is automatic unless
  138.  * samapi was started with /noauto option.  However, you need to select
  139.  * country to access database by record number, name record number,
  140.  * qth searches, etc.
  141.  */
  142.  
  143. int SetCountrySam(int country)
  144. {
  145.     chdr_t cmd;
  146.     rspnumrecs_t resp;
  147.     int old;
  148.  
  149.     if (LocateSam())
  150.         return -1;
  151.  
  152.     old = SamCountry;        /* save in case can't switch */
  153.     SamCountry = country;
  154.  
  155.     /* get Number of records for new country ...
  156.        * if no error, must have been ok to switch
  157.        */
  158.  
  159.     if (CallSam(SamGetNumRecs, &cmd, &resp))
  160.     {
  161.         SamCountry = old;        /* error, so don't switch */
  162.         return -1;
  163.     }
  164.     return 0;
  165. }
  166. #endif
  167.